package com.ebupt.migu.music.system.controller;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.mapper.CommonMapper;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.system.service.RoleService;
import com.ebupt.migu.music.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 部门信息
 *
 * @author liuzzs
 */
@EnableAutoConfiguration
@Controller
@RequestMapping("/system/user")
public class UserController {

    /**
     * 这样使用，request每次进来都是Current Request
     */
    @Autowired
    HttpServletRequest request;

    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    CommonMapper commonMapper;

    @RequestMapping("/list")
    @ResponseBody
    public ResultObject<Object> list() {
        ResultObject<Object> retObj = new ResultObject<>();

        Map<String, Object> paramMap = ReqestParamUtil.getParams(request);

        retObj.setData(userService.queryByPage(paramMap));

        return retObj;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultObject<Object> add() {
        ResultObject<Object> retObj = new ResultObject<Object>();
        Map<String, Object> retMap = new HashMap<>();
        // 获取用户信息和业务参数
        Map<String, Object> paramMap = AuthBaseUtils.authBaseInfo(request);

        String loginName = paramMap.get("loginName").toString();
        String mobile = paramMap.get("mobile").toString();

        SysUser user = userService.getUserByLoginName(loginName);
        if (user != null) {
            throw new MyException(StatusEnum.USER_LOGIN_NAME_EXISTS_ERROR, "");
        }

        user = userService.getUserByMobile(mobile);
        if (user != null) {
            throw new MyException(StatusEnum.USER_MOBILE_EXISTS_ERROR, "");
        }

        String userId = userService.addUser(paramMap);
        retMap.put("userId", userId);

        retObj.setMsg("添加用户成功");
        retObj.setData(retMap);

        return retObj;
    }

    @RequestMapping("/modify")
    @ResponseBody
    public ResultObject<Object> modify() {
        ResultObject<Object> retObj = new ResultObject<Object>();
        Map<String, Object> retMap = new HashMap<>();
        Map<String, Object> paramMap = ReqestParamUtil.getParams(request);

        String userId = userService.modifyUser(paramMap);

        retMap.put("userId", userId);
        retObj.setMsg("修改用户成功");
        retObj.setData(retMap);

        return retObj;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultObject<Object> delete() {
        ResultObject<Object> retObj = new ResultObject<Object>();
        Map<String, Object> paramMap = ReqestParamUtil.getParams(request);

        Integer ret = userService.delUser(paramMap);

        retObj.setMsg("删除用户成功");

        return retObj;
    }

    /**
     * 用户修改密码
     *
     * @return
     */
    @RequestMapping("/updatepwd")
    @ResponseBody
    public ResultObject<Object> updatePWD() {
        Map<String, Object> paramMap = ReqestParamUtil.getParams(request);
        return userService.updatePWD(paramMap);
    }

    /**
     * 分省运营平台同步账号至服务云
     *
     * @param sysUser 账号信息
     * @return 同步结果
     */
    @PostMapping("/syncAccount")
    @ResponseBody
    public ResultObject<String> syncAccount(@RequestBody SysUser sysUser) {
        return userService.syncAccount(sysUser);
    }

    /**
     * 查询邮箱列表
     * 
     * @return
     */
    @GetMapping("/emails")
    @ResponseBody
    public ResultObject<Object> emails() {
        ResultObject<Object> retObj = new ResultObject<>();

        retObj.setData(userService.emails());

        return retObj;
    }
}